import { queryFakeList, queryFakeCart } from './service';
import { Cart, IntroduceProps } from './typing'

export interface StateType {
  list: IntroduceProps[];
  data: Cart[];
}

export interface ModelType {
  namespace: string;
  state: StateType;
  effects: {
    fetchList: any;
    fetchCart: any;
  };
  reducers: {
    queryList: any;
    queryCarts: any;
  };
}

const Model: ModelType = {
  namespace: 'medicalMedicines',

  state: {
    list: [],
    data: [],
  },

  effects: {
    *fetchList({ payload }: any, { call, put }: any) {
      const response = yield call(queryFakeList, payload);
      yield put({
        type: 'queryList',
        payload: Array.isArray(response) ? response : [],
      });
    },
    *fetchCart({ payload }: any, { call, put }: any) {
      const response = yield call(queryFakeCart, payload);
      yield put({
        type: 'queryCarts',
        payload: Array.isArray(response) ? response : [],
      });
    },
  },

  reducers: {
    queryList(state: any, action: any) {
      return {
        ...state,
        list: action.payload,
      };
    },
    queryCarts(state: any, action: any) {
      return {
        ...state,
        data: action.payload,
      };
    },
  },
};

export default Model;
